package com.wxyzz.cem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxyzz.cem.enums.ThreadLocalName;
import com.wxyzz.cem.exception.BalanceApplyServiceException;
import com.wxyzz.cem.interceptor.UserInterceptor;
import com.wxyzz.cem.pojo.BalanceApply;
import com.wxyzz.cem.pojo.User;
import com.wxyzz.cem.pojo.enums.UserRole;
import com.wxyzz.cem.service.BalanceApplyService;
import com.wxyzz.cem.util.Result;
import com.wxyzz.cem.util.ThreadLocalUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 班费申请 前端控制器
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/balance-apply")
public class BalanceApplyController {
    @Autowired
    private BalanceApplyService balanceApplyService;
    
    @PostMapping
    @UserInterceptor.UserIntercept
    public Result apply(@RequestBody BalanceApply balanceApply) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        balanceApply.setUserId(user.getId());
        balanceApply.setClassId(user.getClassId());
        balanceApplyService.apply(balanceApply);
        return Result.success("申请完成，请等待申请通过！");
    }
    
    @PutMapping("/agree")
    @UserInterceptor.UserIntercept
    public Result accept(@RequestBody BalanceApply balanceApply) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        if (user.getRole() != UserRole.CLASS_ADMIN) {
            throw new BalanceApplyServiceException("您不是班级管理员！");
        }
        balanceApply.setUserId(user.getId());
        balanceApply.setClassId(user.getClassId());
        balanceApplyService.accept(balanceApply);
        return Result.success();
    }
    
    @PutMapping("/refuse")
    @UserInterceptor.UserIntercept
    public Result refuse(@RequestBody BalanceApply balanceApply) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        if (user.getRole() != UserRole.CLASS_ADMIN) {
            throw new BalanceApplyServiceException("您不是班级管理员！");
        }
        balanceApply.setUserId(user.getId());
        balanceApply.setClassId(user.getClassId());
        balanceApplyService.refuse(balanceApply);
        return Result.success();
    }
    
    @PutMapping("/revoke")
    @UserInterceptor.UserIntercept
    public Result cancel(@RequestBody BalanceApply balanceApply) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        balanceApply.setUserId(user.getId());
        balanceApply.setClassId(user.getClassId());
        balanceApplyService.cancel(balanceApply);
        return Result.success();
    }
    
    @GetMapping("/page")
    @UserInterceptor.UserIntercept
    public Result<Page<BalanceApply>> page(@RequestParam(required = false, defaultValue = "1") Integer page,
                                           @RequestParam(required = false, defaultValue = "10") Integer size) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        Page<BalanceApply> data = balanceApplyService.pageData(page, size, user.getClassId());
        return Result.success(data);
    }
}
